<project name="extjs" default="build" basedir=".">
    <target name="find-cmd" unless="cmd.dir">
        <!--
        Run "sencha which" to find the Sencha Cmd basedir and get "cmd.dir" setup. We
        need to execute the command with curdir set properly for Cmd to pick up that we
        are running for an application.
        -->
        <exec executable="sencha" dir="${basedir}">
            <arg value="which"/><arg value="-o=$cmddir$"/>
        </exec>

        <!-- Now read the generated properties file and delete it -->
        <property file="$cmddir$"/>
        <delete file="$cmddir$"/>
    </target>

    <target name="init-antcontrib" depends="find-cmd">
        <echo>Using Sencha Cmd from ${cmd.dir}</echo>

        <taskdef resource="net/sf/antcontrib/antlib.xml">
            <classpath>
                <pathelement location="${cmd.dir}/lib/ant-contrib-1.0b3.jar"/>
                <pathelement location="${cmd.dir}/lib/commons-httpclient-3.0.1.jar"/>
                <pathelement location="${cmd.dir}/lib/commons-logging-1.0.4.jar"/>
                <pathelement location="${cmd.dir}/lib/commons-codec-1.3.jar"/>
            </classpath>
        </taskdef>
    </target>

    <target name="init-sencha-cmd" depends="init-antcontrib">
        <taskdef resource="com/sencha/ant/antlib.xml" classpath="${cmd.dir}/sencha.jar"/>
    </target>

    <target name="init-all" depends="init-sencha-cmd">
        <property name="build.dir"          location="${basedir}"/>
        <property name="build.docs.dir"     location="${build.dir}/docs"/>

        <echo>build.dir: ${build.dir}</echo>
    </target>

    <!-- ****************************************************************** -->

    <target name="build" depends="init-all"
            description="Build the SDK from source">
        <!--
        Lay down the file header so we can append the rest from the compiler.
        -->
        <for list="ext-core,ext-foundation,ext-all-sandbox,ext-all-rtl-sandbox" param="file">
            <sequential>
                <for list=".js,-dev.js,-debug-w-comments.js" param="sfx">
                    <sequential>
                        <copy file="${build.dir}/file-header.js"
                              tofile="${build.dir}/builds/@{file}@{sfx}" overwrite="true"/>
                    </sequential>
                </for>
            </sequential>
        </for>
        <for list="ext,ext-all,ext-all-rtl" param="file">
            <sequential>
                <for list=".js,-dev.js,-debug-w-comments.js" param="sfx">
                    <sequential>
                        <copy file="${build.dir}/file-header.js"
                              tofile="${build.dir}/@{file}@{sfx}" overwrite="true"/>
                    </sequential>
                </for>
            </sequential>
        </for>

        <!--
        Compile from sources and appending to stubs containing just the license header.
        -->
        <x-sencha-command dir="${basedir}">
            compile
                -ignore=diag

                # Remove the license header from the source files:
                -prefix
                    ${basedir}/file-header.js

                # Build *-dev.js files - these have all "debug" conditional code active
                # for use in development mode.

                -options=debug:true
                    concatenate
                        +append
                        -output-file=${build.dir}/ext-all-rtl-dev.js
                    and
                    concatenate
                        +append
                        -sandbox=Ext4:x4-
                        -output-file=${build.dir}/builds/ext-all-rtl-sandbox-dev.js
                    and


                    exclude
                        -namespace=Ext.rtl
                    and


                    concatenate
                        +append
                        -output-file=${build.dir}/ext-all-dev.js
                    and
                    concatenate
                        +append
                        -sandbox=Ext4:x4-
                        -output-file=${build.dir}/builds/ext-all-sandbox-dev.js
                    and


                    union
                        -tag=core
                    and
                    concatenate
                        +append
                        -output-file=${build.dir}/ext-dev.js
                    and
                    concatenate
                        +append
                        -output-file=${build.dir}/builds/ext-core-dev.js
                    and


                    union
                        -tag=foundation
                    and
                    concatenate
                        +append
                        -output-file=${build.dir}/builds/ext-foundation-dev.js
                    and

                # Build *-debug-w-comments.js files - these are comment stripped to give
                # *-debug.js files. These have normal whitespace and are intended to be
                # debuggable versions of *-all.js files. They do not contain "dev mode"
                # diagnostic code.
                    
                -options=debug:false
                    include
                        +all
                    and

                    concatenate
                        +append
                        -output-file=${build.dir}/ext-all-rtl-debug-w-comments.js
                    and
                    concatenate
                        +append
                        -sandbox=Ext4:x4-
                        -output-file=${build.dir}/builds/ext-all-rtl-sandbox-debug-w-comments.js
                    and


                    exclude
                        -namespace=Ext.rtl
                    and


                    concatenate
                        +append
                        -output-file=${build.dir}/ext-all-debug-w-comments.js
                    and
                    concatenate
                        +append
                        -sandbox=Ext4:x4-
                        -output-file=${build.dir}/builds/ext-all-sandbox-debug-w-comments.js
                    and


                    union
                        -tag=core
                    and
                    concatenate
                        +append
                        -output-file=${build.dir}/ext-debug-w-comments.js
                    and
                    concatenate
                        +append
                        -output-file=${build.dir}/builds/ext-core-debug-w-comments.js
                    and


                    union
                        -tag=foundation
                    and
                    concatenate
                        +append
                        -output-file=${build.dir}/builds/ext-foundation-debug-w-comments.js
                    and

                # Generate bootstrap data in to ext-dev.js and ext-debug-w-comments.js to
                # enable the dynamic loader.

                    include
                        +all
                    and
                    exclude
                        -tag=core
                    and

                    metadata
                        +append
                        +alternates
                        -base-path=${build.dir}
                        -output-file=${build.dir}/ext-debug-w-comments.js
                    and
                    metadata
                        +append
                        +alias
                        -base-path=${build.dir}
                        -output-file=${build.dir}/ext-debug-w-comments.js
                    and

                    metadata
                        +append
                        +alternates
                        -base-path=${build.dir}
                        -output-file=${build.dir}/ext-dev.js
                    and
                    metadata
                        +append
                        +alias
                        -base-path=${build.dir}
                        -output-file=${build.dir}/ext-dev.js
                    and
            
                # Optimize and compress the builds
            
                -options=debug:false
                    include
                        +all
                    and
                    optimize
                        -define-rewrite
                    and
            
            
                    concatenate
                        +append
                        +yui
                        -output-file=${build.dir}/ext-all-rtl.js
                    and
                    concatenate
                        +append
                        +yui
                        -sandbox=Ext4:x4-
                        -output-file=${build.dir}/builds/ext-all-rtl-sandbox.js
                    and
            
            
                    exclude
                        -namespace=Ext.rtl
                    and
                    concatenate
                        +append
                        +yui
                        -output-file=${build.dir}/ext-all.js
                    and
                    concatenate
                        +append
                        +yui
                        -sandbox=Ext4:x4-
                        -output-file=${build.dir}/builds/ext-all-sandbox.js
                    and
            
            
                    union
                        -tag=core
                    and
                    concatenate
                        +append
                        +yui
                        -output-file=${build.dir}/ext.js
                    and
                    concatenate
                        +append
                        +yui
                        -output-file=${build.dir}/builds/ext-core.js
                    and
            
            
                    union
                        -tag=foundation
                    and
                    concatenate
                        +append
                        +yui
                        -output-file=${build.dir}/builds/ext-foundation.js
            
        </x-sencha-command>

        <!--
        Strip comments and compress all flavors.
        -->
        <for list="ext,ext-all,ext-all-rtl,builds/ext-all-sandbox,builds/ext-all-rtl-sandbox,builds/ext-core,builds/ext-foundation"
             param="kind">
            <sequential>
                <x-strip-js srcfile="${build.dir}/@{kind}-debug-w-comments.js"
                            outfile="${build.dir}/@{kind}-debug.js"/>
            </sequential>
        </for>

    </target>

    <target name="examples" depends="init-all">
        <x-sencha-command>
            compile
                --ignore=diag,rtl/
                --classpath=${basedir}/examples/shared,${basedir}/examples/ux
                --classpath=${basedir}/examples/desktop
                --classpath=${basedir}/examples/portal
                --classpath=${basedir}/examples/grouptabs
                --classpath=${basedir}/examples/kitchensink
                --classpath=${basedir}/examples/app/simple
                --classpath=${basedir}/examples/simple-tasks
                --classpath=${basedir}/examples/app/nested-loading
                --classpath=${basedir}/examples/app/feed-viewer
                --options=debug:false
                    page
                        --scripts=../common.js
                        --input-file=${basedir}/examples/desktop/desktop.html
                        --output=${build.dir}/examples/desktop/compiled-desktop.html
                        --name=desktop
                    and
                    page
                        --scripts=../common.js
                        --input-file=${basedir}/examples/grouptabs/grouptabs.html
                        --output=${build.dir}/examples/grouptabs/compiled-grouptabs.html
                        --name=grouptabs
                    and
                    page
                        --scripts=../common.js
                        --input-file=${basedir}/examples/kitchensink/index.html
                        --output=${build.dir}/examples/kitchensink/compiled-index.html
                        --name=kitchensink
                    and
                    page
                        --scripts=../../common.js
                        --input-file=${basedir}/examples/app/simple/simple.html
                        --output=${build.dir}/examples/app/simple/compiled-simple.html
                        --name=simpleapp
                    and
                    page
                        --scripts=../common.js
                        --input-file=${basedir}/examples/simple-tasks/index.html
                        --output=${build.dir}/examples/simple-tasks/compiled-index.html
                        --name=simpletasks
                    and
                    page
                        --scripts=../../common.js
                        --input-file=${basedir}/examples/app/nested-loading/nested-loading.html
                        --output=${build.dir}/examples/app/nested-loading/compiled-nested-loading.html
                        --name=nested-loading
                    and
                    page
                        --scripts=../../common.js
                        --input-file=${basedir}/examples/app/feed-viewer/feed-viewer.html
                        --output=${build.dir}/examples/app/feed-viewer/compiled-feed-viewer.html
                        --name=feed-viewer
                    and
                    page
                        --scripts=../common.js
                        --input-file=${basedir}/examples/portal/portal.html
                        --output=${build.dir}/examples/portal/compiled-portal.html
                        --name=portal
                    and
                    intersect
                        -min=6
                        -set=desktop,grouptabs,kitchensink,simpleapp,simpletasks,nested-loading,feed-viewer,portal
                    and
                    save
                        common
                    and
                    concatenate
                        --strip-comments=true
                        --output-file=${build.dir}/examples/common.js
                    and
                    restore
                        portal
                    and
                    exclude
                        -set=common
                    and
                    concatenate
                        --output-file=${build.dir}/examples/portal/all-classes.js
                    and
                    restore
                        feed-viewer
                    and
                    exclude
                        -set=common
                    and
                    concatenate
                        --output-file=${build.dir}/examples/app/feed-viewer/all-classes.js
                    and
                    restore
                        nested-loading
                    and
                    exclude
                        -set=common
                    and
                    concatenate
                        --output-file=${build.dir}/examples/app/nested-loading/all-classes.js
                    and
                    restore
                        simpletasks
                    and
                    exclude
                        -set=common
                    and
                    concatenate
                        --output-file=${build.dir}/examples/simple-tasks/all-classes.js
                    and
                    restore
                        simpleapp
                    and
                    exclude
                        -set=common
                    and
                    concatenate
                        --output-file=${build.dir}/examples/app/simple/all-classes.js
                    and
                    restore
                        kitchensink
                    and
                    exclude
                        -set=common
                    and
                    concatenate
                        --output-file=${build.dir}/examples/kitchensink/all-classes.js
                    and
                    restore
                        grouptabs
                    and
                    exclude
                        -set=common
                    and
                    concatenate
                        --output-file=${build.dir}/examples/grouptabs/all-classes.js
                    and
                    restore
                        desktop
                    and
                    exclude
                        -set=common
                    and
                    concatenate
                        --output-file=${build.dir}/examples/desktop/all-classes.js
        </x-sencha-command>
    </target>
</project>
